package com.example.lib_net

import com.example.lib_net.common.ConstantValue
import com.example.lib_net.interceptor.BasicParamsInterceptor
import com.example.lib_net.interceptor.HeaderInterceptor
import com.example.lib_net.interceptor.RequestCacheInterceptor
import com.example.lib_net.interceptor.ResponseCacheInterceptor
import com.example.lib_net.interceptor.RetryInterceptor
import com.example.lib_net.interceptor.SignInterceptor
import com.example.lib_net.interceptor.TokenInterceptor
import com.example.lib_net.msgbus.LiveDataBus
import com.tencent.mmkv.MMKV
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/24
 */
class RetrofitFactory private constructor(){

    private val retrofit:Retrofit

    fun createCache(): Cache {
        var file = File("/sdcard/Music")
        return Cache(file,10*1024*1024)
    }
    init {
        retrofit=Retrofit.Builder()
            .baseUrl("http://10.161.9.80:7015")
            .addConverterFactory(GsonConverterFactory.create())
//            .addCallAdapterFactory(CoroutineCallAdapterFactory.create)
            .client(createOkHttpClient())
            .build()
    }

    /**
     * Create OkHttp Client
     */
    private fun createOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
            .addInterceptor(TokenInterceptor()) //token拦截器
            .addInterceptor(SignInterceptor()) //签名sign拦截器
            .addInterceptor(HeaderInterceptor()) //请求头拦截器
            .addInterceptor(BasicParamsInterceptor()) //请求参数拦截器
            .addInterceptor(RetryInterceptor(5))//失败重试拦截器
            .cache(createCache()) //配置缓存路径
            .addInterceptor(RequestCacheInterceptor()) //缓存拦截器
            .addNetworkInterceptor(ResponseCacheInterceptor()) //缓存拦截器
            .addInterceptor(HttpLoggingInterceptor()) //日志拦截器
//            .addInterceptor(createNormalErrorHttpCodeInterceptor())
            .sslSocketFactory(//https证书认证
                SSLSocketFactoryUtil.createSSLSocketFactory(),
                SSLSocketFactoryUtil.createTrustAllManager()
            )
            .hostnameVerifier(SSLSocketFactoryUtil.TrustAllHostnameVerifier())//域名认证
            .build()
    }


    /**
     * create logging interceptor and set level
     */
    private fun createLoggingInterceptor(): Interceptor {
        return HttpLoggingInterceptor().apply { level= HttpLoggingInterceptor.Level.BODY }
    }






    companion object{
        //双重校验锁
        private val instance:RetrofitFactory by lazy(LazyThreadSafetyMode.SYNCHRONIZED){ RetrofitFactory()}
        val TIMEOUT=30L
        fun getRetrofitFactory():RetrofitFactory{
            return instance
        }
    }

    fun <T>create(service:Class<T>):T{
        return retrofit.create(service)
    }


}